<template>
  <van-list
    :immediate-check="false"
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    :error.sync="error"
    error-text="请求失败，点击重新加载"
    @load="onLoad"
  >
    <commentItem
      v-for="(item, index) in list"
      :key="index"
      :comment="item"
      @onreply="$emit('onreply', $event)"
    />
  </van-list>
</template>

<script>
import { getArtCommentsList } from '@/api/comment'
import commentItem from '../commentItem'
export default {
  props: ['type', 'source', 'list'],
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      error: false
    }
  },

  created () {
    this.onLoad()
    this.list.length = 0
  },
  components: {
    commentItem
  },
  methods: {
    async onLoad () {
      try {
        // 1发请求
        const res = await getArtCommentsList({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: 10
        })
        this.$emit('count', res.data.data)
        // 2本地追加
        this.list.push(...res.data.data.results)
        // 3loding=false
        this.loading = false
        // 4判断
        if (this.list.length < res.data.data.total_count) {
          this.offset = res.data.data.last_id
        } else {
          this.finished = true
        }
      } catch (error) {
        this.loading = false
        this.error = true
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
